/************************公告管理js*********************************/
define(function(require) {
	
	var $ = require("jquery1.7");
	var internalEmailService = require("bean/internalEmailService");
	var utils = require("utils");
	var ctx = utils.constants.ctx;
	var ui = require("ui");
	
	var BlackBox = require("blackbox");
	var blackBox = new BlackBox();
	
	$(function(){
		var inx = $(".p_publisher").length;
		for(var i=0;i<inx;i++){
			var id = $(".p_publisher").eq(i).attr("data");
			var result = internalEmailService.findLinkManById(id);
			var htm =""
			for(var j=0;j<result.data.length;j++){
				htm += result.data[j].nickname + ",";
			}
			$(".p_publisher").eq(i).html(htm);
		}
	});
	
	//添加邮件
	$("#addEmailBtn").bind("click",function(){
		location.href = ctx + "/back/website/emailContent";
	});
	
	//批量删除
	$("#deleteEmailBtn").bind("click", function(){
		if ($('input[name=items]:checkbox:checked').length <= 0) {
			blackBox.alert("请选择邮件");
			return;
		}
		blackBox.confirm("确定要删除邮件？",function(isOk){
			if(isOk){
				var array = [];
				$('input[name=items]:checkbox:checked').each(function() {
					
					var id = $(this).attr("id").replace("email_", "");
					array.push(id);   
				});
				if (array.length != 0) {
					try{
						ui.mask().show("请稍候...");
						internalEmailService.deleteEmail(array);
						ui.mask().hide();
						blackBox.alert("删除成功");
						window.location.reload(true);
					} catch (e) {
						ui.mask().hide();
						blackBox.alert("删除失败:"+e.message);
					}
				}
			}
		},{
			title:'提示',
			value:'确认'
		});
	});
	
	//发送
	$("#issueEmailBtn").bind("click",function(){
		if ($('input[name=items]:checkbox:checked').length != 1) {
			blackBox.alert("请选择一条邮件");
			return;
		}
		
		blackBox.confirm("确定要发送？",function(isOk){
			if(isOk){
				var email_id = $('input[name=items]:checkbox:checked').attr("id").replace("email_", "");
				try{
					ui.mask().show("请稍候...");
					internalEmailService.issueNotice(email_id);
					ui.mask().hide();
					blackBox.alert("发送成功");
					window.location.reload(true);
				}catch (e) {
					ui.mask().hide();
					blackBox.alert("发送失败:"+e.message);
				}
			}
		},{
			title:'提示',
			value:'确认'
		});
	});
	
	
	//修改邮件
	$(".updateEmail").bind("click",function(){
		var id = $(this).attr("data").replace("email_", "");
		var issueStatus = $(this).parent().parent().parent().prev().html();
		if(issueStatus=="已发送"){
			blackBox.alert("已发送的邮件不可修改");
			return;
		}
		location.href = ctx + "/back/website/emailContent?emailId="+id;
	});
	
	//查看邮件详细信息
	$(".checkEmailDetail").click(function(){
		var id = $(this).attr("data").replace("email_", "");

		location.href = ctx + "/back/website/emailDetail?emailId="+id;
	});
	
	//查询邮件
	$("#email_searchBtn").click(function(){
		var name=$("#eamil_search_keyword").val();
		if(name==null){
			blackBox.alert("请输入公告标题");
			return;
		}
		location.href=ctx+"/back/website/internalEmailManager?name="+name;
	});
	
	//撤回邮件
	$(".abolishEmail").click(function(){
		var id = $(this).attr("data").replace("email_", "");
		internalEmailService.abolishEmail(id);
		blackBox.alert("撤回成功！！！");
		location = location;
	});
});

